package com.example.testapp.activity.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.model.ResponseWrapper;
import com.example.testapp.activity.view.CHImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by l on 2016/8/3.
 */
public class ListWeatherAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ResponseWrapper.WeaterEntity.WeatherSubEntity> mWeatherList = new ArrayList<>();

    public ListWeatherAdapter(Context context, List<ResponseWrapper.WeaterEntity.WeatherSubEntity> weather_data) {
        this.inflater = LayoutInflater.from(context);
        this.mWeatherList = weather_data;
    }

    @Override
    public int getCount() {
        return mWeatherList == null ? 0 : mWeatherList.size();
    }

    @Override
    public Object getItem(int position) {
        return mWeatherList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.view_weather_item, parent, false);
            holder.dateTextView = (TextView)convertView.findViewById(R.id.list_date_nextday);
            holder.listdayImage = (CHImageView)convertView.findViewById(R.id.list_day_picture);
            holder.nightImage = (CHImageView)convertView.findViewById(R.id.list_night_picture);
            holder.weatherTV = (TextView) convertView.findViewById(R.id.list_weather);
            holder.tempTV = (TextView) convertView.findViewById(R.id.list_temperature);
            holder.windTV = (TextView) convertView.findViewById(R.id.list_wind);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        //日期
        if(position > 0){
            holder.dateTextView.setText(mWeatherList.get(position).getDate());
        }else if(position == 0){
            holder.dateTextView.setText("今天");
        }

        // 白天天气图片
        String dayp = mWeatherList.get(position).getDayPictureUrl();
        holder.listdayImage.loadImage(dayp);

        // 夜晚天气图片
        String nightp = mWeatherList.get(position).getNightPictureUrl();
        holder.nightImage.loadImage(nightp);

        // 天气情况
        holder.weatherTV.setText(mWeatherList.get(position).getWeather());

        // 温度;
        String temp = mWeatherList.get(position).getTemperature();
        if(temp.length() > 4){
            //默认将最低温度显示在前面的算法
            String str1 = temp.substring(0, temp.length() - 1);
            String str2 = temp.substring(temp.length() - 1, temp.length());
            Log.i("TAG", str2);
            String[] mstr = str1.split("~", 2);
            holder.tempTV.setText(mstr[1] + "~" + mstr[0] + str2);
        }else {
            holder.tempTV.setText(temp);
        }

        holder.windTV.setText(mWeatherList.get(position).getWind());

        return convertView;
    }

    public class ViewHolder{
        public TextView dateTextView;
        public CHImageView listdayImage;
        public CHImageView nightImage;
        public TextView  weatherTV;
        public TextView  tempTV;
        public TextView  windTV;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }
}
